 package com.tanxiaoyao.wecon.ui;

import com.tanxiaoyao.wecon.R;
import com.tanxiaoyao.wecon.app.AppManager;
import com.tanxiaoyao.wecon.net.GlobalConnectHelper;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

/**
 * 重力传感手柄界面
 * 
 * @author tanxiaoyao (junlidev@gmail.com)
 * @version 2015年2月17日 下午8:22:31
 */
public class ActivityGravityHandle extends Activity {

	// DEBUG标志
	private static final String TAG = "tanxiaoyao-gravity-handle";
	private static final Boolean ISDEBUG = false;

	private Button breakBtn, boostBtn;

	//旋转图片所用矩阵
	private Matrix matrix = new Matrix();
	//旋转图片
	private ImageView handle_pos_view;
	
	// 全局发送器
	private GlobalConnectHelper globalConnectHelper;
	private final String GH_ACCER = "gra_han_accer:";
	private final String GH_BREAK = "gra_han_break";
	private final String GH_BOOST = "gra_han_boost";
	private final String DOWN = "_down";
	private final String UP = "_up";

	// 声明传感器管理器
	private SensorManager sensorManager;
	private CheckBox isGravity;
	private Sensor accerSensor;
	private int index = 0;	//抽样计数值
	private static final int DELAY_DISTANCE = 10;	//界面刷新速率为传感器的1/10
	SensorEventListener sensorListener = new SensorEventListener() {

		@Override
		public void onSensorChanged(SensorEvent event) {
			globalConnectHelper.sendMess(GH_ACCER
					+ String.valueOf(isGravity.isChecked() ? event.values[0] : 25) + ","
					+ String.valueOf(event.values[1]) + ","
					+ String.valueOf(event.values[2]));
			//下抽样刷新界面，防止UI卡死
			index ++;
			if(index % DELAY_DISTANCE == 0){
				rotateImage(event);
			}
			if(index >= 60000){
				index = 0;	//防止int数过大移除
			}
		}

		private void rotateImage(SensorEvent event) {
			Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.handle_pos_view)).getBitmap();
            // 设置旋转角度  
            matrix.setRotate(event.values[1] * 9);  
            // 重新绘制Bitmap  
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);
            handle_pos_view.setImageBitmap(bitmap);
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_gravity_handle);
//      Activity压栈
      AppManager.getAppManager().addActivity(this);
		// 初始化控件
		breakBtn = (Button) findViewById(R.id.btn_gravity_break);
		boostBtn = (Button) findViewById(R.id.btn_gravity_boost);
		// dialView = (DialView) findViewById(R.id.dial_view);
		isGravity = (CheckBox) findViewById(R.id.chebox_is_gravity);
		handle_pos_view = (ImageView) findViewById(R.id.handle_pos_image);
		// 按键监听器
		boostBtn.setOnTouchListener(btnOnTouchListener);
		breakBtn.setOnTouchListener(btnOnTouchListener);
		isGravity.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked || accerSensor == null) {
					accerSensor = sensorManager
							.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
					sensorManager.registerListener(sensorListener, accerSensor,
							SensorManager.SENSOR_DELAY_GAME);
				}

			}
		});
		// 初始化发送器
		globalConnectHelper = GlobalConnectHelper.getGlobalConnector();
		// 初始化传感器管理器
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		// 判断是否存在加速度传感器
		if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) == null) {
			Toast.makeText(this, getString(R.string.has_no_related_ssensors),
					Toast.LENGTH_LONG).show();
		} else {
			// 拥有相关传感器，开启重力传感
				accerSensor = sensorManager
						.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
				sensorManager.registerListener(sensorListener, accerSensor,
						SensorManager.SENSOR_DELAY_GAME);
		}
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStop() {
		// 注销传感器监听器
		sensorManager.unregisterListener(sensorListener, accerSensor);
		super.onStop();
	}

	/**
	 * 控件监听器
	 */
	OnTouchListener btnOnTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			final boolean boostPressed;
			if (!isGravity.isChecked()) {
				switch (v.getId()) {
				case R.id.btn_gravity_boost:
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						globalConnectHelper.sendMess(GH_BOOST + DOWN);
					}else if (event.getAction() == MotionEvent.ACTION_UP) {
						globalConnectHelper.sendMess(GH_BOOST + UP);
					}

					break;
					case R.id.btn_gravity_break:
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							globalConnectHelper.sendMess(GH_BREAK + DOWN);
						}else if (event.getAction() == MotionEvent.ACTION_UP) {
							globalConnectHelper.sendMess(GH_BREAK + UP);
						}
							break;
				default:
					break;
				}
			}
			return false;
		}
	};

}
